package lib

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"regexp"
	"strings"
)

type Lolicon struct {
	Error string
	Data  []struct {
		Title string
		Urls  struct {
			Original string
		}
	}
}

type TBData struct {
	Title string
	Pic   string
}

func RequestLolicon(url string, i *Lolicon) (err error) {
	client := &http.Client{}
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		return
	}
	req.Header.Add("User-Agent", getAgent())
	resp, err := client.Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()
	// api
	body, err := ioutil.ReadAll(resp.Body)
	json.Unmarshal(body, &i)
	return
}

func RequestTB(url string, i *TBData) (err error) {
	client := &http.Client{}
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		return
	}
	req.Header.Add("User-Agent", getAgent())
	resp, err := client.Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()
	// api
	body, err := ioutil.ReadAll(resp.Body)
	json.Unmarshal(body, &i)
	return
}

func GetFuliShe() (url string) {
	re := regexp.MustCompile(`\nhttps?://(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*`)
	resp, err := HttpGet("http://shengapi.cn/api/ppgx.php?lx=女神福利社")
	if err != nil {
		return
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	res := re.Find(body)
	return strings.Replace(string(res), "\n", "", -1)
}
